export interface NgaLocation {
    tid: number | undefined
    pid: number | undefined
    page: 'e' | number | undefined
    fid: number | undefined
}

export const parseLocation = (uri: string): NgaLocation => {
    const url = new URL(`https://bbs.nga.cn${uri}`);
    const params = url.searchParams;

    const getId = (key: string) => {
        const value = params.get(key);
        const number = Number(value)
        return (value && !isNaN(number)) ? number : undefined
    }


    const tid = getId("tid")
    const pid = Number(url.hash.match(/pid(\d+)Anchor/)?.[1] || '0') || getId("pid")
    const fid = getId("fid") || getId("_ff")
    const page = params.get("page") === 'e' ? 'e' : getId("page")
    return {tid, pid, page, fid}
}